<?php
class Language_Controller extends AdminController {
    
    public function __construct() {
        parent::__construct('language');
        $this->setDefaultListOrder('name');
    }
    
    function all() {
        $this->initPagination('language');
        
        $this->viewData['languageList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM language'));
        
        $this->renderSubview('admin/language/languageList');
    }
    
    function display() {
        $code = $this->input->get('code');
        
        if (! valid::isCode($code)) {
            $this->redirectToList('error.invalidId');
        }

        $language = $this->dao->getByCode('language', $code);
        if (! $language) {
            $this->redirectToList('error.recordNotFound');
        }
        
        $this->viewData['language'] = $language;
        $this->renderSubview('admin/language/languageDisplay');
    }
    
    function edit() {
        $code = $this->input->get('code');
        $language = array();
        
        if ($code) {
            $language = $this->dao->getByCode('language', $code);
            
            if (! $language) {
                $this->redirectToList('error.recordNotFound');
            }
        }
        else {
            // Set default insert values here
        }
        
        $this->viewData['language'] = $language;
        $this->renderSubview('admin/language/languageEdit');
    }
    
    function save() {
        $language = $this->postToDbValues();
        $this->viewData['language'] = $language;
        
        // Basic validation
        $validator = new EntityValidator($language, 'language', $this->msg);
        $validator->required('code');
        $validator->required('name');
        $validator->required('name_native');
        
        if ($this->msg->hasErrors()) {
            $this->publishErrors('admin/language/languageEdit');
            return;
        }

        // Database validation
        if ($this->dao->alreadyExists('language', 'name', $language, 'code')) {
            $this->msg->error('language.error-alreadyExists-name', $language['name']);
        }
        if (! sql::isPersistent($language)) {
            $existingObject = $this->dao->getByCode('language', $code);
            if ($existingObject) {
                $this->msg->error('language.error-alreadyExists-code', $language['code']);
            }
        }
        if ($this->msg->hasErrors()) {
            $this->renderSubview('admin/language/languageEdit');
            return;
        }

        // Insert / Update
        if (sql::isPersistent($language)) {
            $this->dao->update('language', $language, 'code');
            $this->msg->info('language.editSuccess', $language['name']);
        }
        else {
            $this->dao->insert('language', $language);
            $this->msg->info('language.createSuccess', $language['name']);
        }
        
        $this->ajaxSuccessOrRedirect('/admin/language/all', $language);
    }
    
    function delete() {
        $code = $this->input->get('code');

        if (! valid::isCode($code)) {
            $this->redirectToList('error.invalidId');
        }

        $language = $this->dao->getByCode('language', $code);
        if (! $language) {
            $this->redirectToList('error.recordNotFound');
        }
        
        $this->dao->deleteByCode('language', $code);
        
        $this->msg->info('language.deleteSuccess', $language['name']);
        $this->redirectToList();
    }
}
?>
